<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>寝室选择</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            {{tishi}}
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column prop="id" label="序号" align="center"></el-table-column>
                <el-table-column prop="dormitoryName" label="寝室号"   align="center"></el-table-column>
                <el-table-column prop="allBed" label="寝室总床位"  align="center"></el-table-column>
                <el-table-column prop="surplusBed"  label="剩余床位"  align="center">

                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button id="btn" type="primary" size="mini" @click="ruZhu(scope.row)">入住该寝室</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js" defer="true"></script>
<script src="../js/axios-0.18.0.js" ></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            dataList: [],//当前页要展示的分页列表数据
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //查询寝室数据
            findPage() {
                var stuId = sessionStorage.getItem("stuId");
                axios.get("/dormitory/findDormitoryByStuId?id="+stuId).then((res)=>{
                    this.dataList=res.data.rows;
                });
                axios.get("/student/findById?id="+stuId).then((response)=>{
                    var stuD = response.data.data.stuDormitory;
                    if(stuD != null){
                        //document.getElementById("btn").disabled=true;
                        this.tishi = "你已经有寝室了，不能再继续选择";
                    }
                })
            },
            ruZhu(row){
                console.log(row.surplusBed);
                var surplusBed = row.surplusBed;
                if(surplusBed <=0){
                    this.$message.error("小可爱,没有剩余床位啦,换个寝室吧");
                    return;
                };
                var stuID = sessionStorage.getItem("stuId");
                axios.post("/dormitory/selectDormitory",{stuId:stuID,dormitoryId:row.id}).then((response)=>{
                    if(response.data.flag){
                        this.$message({
                            message: response.data.message,
                            type: 'success'
                        });
                    }else{
                        this.$message.error(response.data.message);
                    }
                });
            }

        }
    })
</script>
</html>